const memoize = require("./memoize") 

const lazyFunction = factory => {
	const fac = memoize(factory);
	const f = /** @type {any} */ (
		(...args) => {
			return fac()(...args);
		}
	);
	return /** @type {T} */ (f);
};

const mergeExports = (obj, exports) => {
  // Object.getOwnPropertyDescriptors()方法：用来获取一个对象的所有自身属性的描述符
    const descriptors = Object.getOwnPropertyDescriptors(exports)
    for (const name of Object.keys(descriptors)) {
        const descriptor = descriptors[name]
        if(descriptor.get){
            const fn = descriptor.get
            Object.defineProperty(obj, name, {
                configurable: false,
                enumerable: true,
                get: memoize(fn)
            })
        } else if (typeof descriptor.value === 'object'){
            Object.defineProperties(obj, name, {
                configurable : false,
                enumerable: true,
                writable: false,
                value: mergeExports({}, descriptor.value)
            })
        }
    }
    return Object.freeze(obj)
}

const fn = lazyFunction(() => require("./webpack"));
module.exports = mergeExports(fn, {
    get webpack() {
		return require("./webpack");
	},
    get PrefetchPlugin() {
		return require("./PrefetchPlugin");
	},
})
